<?php
/**
 * Created by PhpStorm.
 * User: xudongsheng
 * Date: 2017/9/21
 * Time: 上午9:06
 */

namespace app\admin\model;


class Config extends \think\Model
{
    /** 配置 分组
     * @var array
     */
    public static $groups = [
        ['groups_id' =>  1,'title'     =>  '站点配置','icon'=>'fa fa-laptop'],
        ['groups_id' =>  2,'title'     =>  '核心配置','icon'=>'fa fa-laptop'],
        ['groups_id' =>  3,'title'     =>  '邮箱配置','icon'=>'fa fa-laptop'],
        ['groups_id' =>  4,'title'     =>  '上传配置','icon'=>'fa fa-laptop'],
        ['groups_id' =>  5,'title'     =>  '会员配置','icon'=>'fa fa-laptop'],
        ['groups_id' =>  6,'title'     =>  '其他配置','icon'=>'fa fa-laptop']
    ];
    /** 数据类型
     * @var array
     */
    public static $types   = [
        ['type_id'  =>  1,'title'   =>  '字符','template' =>  'text'],
        ['type_id'  =>  2,'title'   =>  '数字','template' =>  'text'],
        ['type_id'  =>  3,'title'   =>  '文本','template' =>  'textarea'],
        ['type_id'  =>  4,'title'   =>  '布尔型','template' =>  'radio'],
        ['type_id'  =>  5,'title'   =>  '文件','template' =>  'file']
    ];
    /** 根据分组 获取 所有或者分组的  配置
     * @param null $groups
     * @return false|\PDOStatement|string|\think\Collection
     */
    public static function configList($groups = NULL){
        $where  =   [];
        (!is_null($groups) AND is_numeric($groups))?($where['groups'] = $groups):'';
        $data   =   self::where($where)->order('sort','desc')->paginate(15);
        foreach ($data as &$v){
            $v['groups_text']   =   self::getGroup($v['groups'],'title');
            $v['type_text']     =   self::getType($v['type'],'title');
        }
        return   $data;
    }

    /** 查出所有配置
     * @param bool $origin
     * @return mixed
     */
    public static function showConfig($origin = false,$html =   true){
        $result['data']   =   self::where('status',1)->order('sort','desc')->select();
        if ($html){
            $result['data']   =   self::groupsData($result['data']);
            foreach ($result['data'] as $k => &$config){
                foreach ($config as &$v){
                    $origin?($v['html'] =  self::formatHtml($v)):($v = self::formatHtml($v));
                }
                $result['title'][]  = self::getGroup($k);
            }
        }
        return $result;
    }

    public static function getConf(){
        $conf   =   self::showConfig(true,false);
        $tmp    =   [];
        foreach ($conf['data'] as $v){
            $tmp[$v['mark']]    =   $v['value'];
        }
        return $tmp;
    }

    //根据数据 格式化  HTML
//    public static function format($data,$origin = false){
//        foreach ($data as &$v){
////            if ('' === $v['form_view'] OR empty($v['form_view'])){
////                $template   = '';
////                foreach (self::$types as $type){
////                    if ($type['type_id']  ==  $v['type']){
////                        $template   =   $type['template'];
////                        break;
////                    }
////                }
////                $v['html']  =   self::formatHtml($template,$v);
////            }else{
////                $v['form_view'] =   unserialize($v['form_view']);
////                $v['html']  =   self::formatHtml($template,$v);
////            }
//            self::formatHtml($v);
//        }
////        var_dump($data);
//
//    }

    private static function formatHtml($config){
        if ('' !== $config['form_view'] AND !empty($config['form_view'])){
            $config['form_view']    =   unserialize($config['form_view']);  // array
        }
        $template    =  'n_';
        $template   .=  is_array($config['form_view'])?$config['form_view']['element']:self::getType($config['type'],'template');
        $temp        =  '<div class="form-group"><label class="col-sm-1 control-label">'.$config['name'].'</label><div class="col-sm-11">';
        $temp       .=  self::$template($config);
        $temp       .=  $config['description']?'<span class="help-block m-b-none">'.$config['description'].'</span>':'';
        $temp       .=  '</div></div><div class="hr-line-dashed"></div>';
        return $temp;
    }

    /** 表单 text
     * @param $config
     * @return string
     */
    private static function n_text($config){
        $temp  = '<input type="text" value="'.$config['value'].'" class="form-control" name="config['.$config['mark'].']" />';
//        $temp .= $config['description']?'<span class="help-block m-b-none">'.$config['description'].'</span>':'';
        return $temp;
    }

    /** 文件
     * @param $config
     * @return string
     */
    private static function n_file($config){
        $temp = '<div class="col-sm-4">';
        $temp .= '<input type="text" value="'.$config['value'].'" id="'.$config['mark'].'" class="form-control" name="config['.$config['mark'].']" />';
        $temp .= '</div>';
        $temp .= '<div class="col-sm-6">';
//        $temp .= '<input type="file" name="file"/>';
        $temp .= '<div class="uploader" data-id="#'.$config['mark'].'">'.$config['name'].'</div>';
        $temp .= '</div>';
//        $config['description']?($temp .= '<span class="help-block m-b-none">'.$config['description'].'</span>'):'';
        return $temp;
    }

    /** 表单 textarea
     * @param $config
     * @return string
     */
    private static function n_textarea($config){
        $temp =  '<textarea class="form-control" name="config['.$config['mark'].']">'.$config['value'].'</textarea>';
//        $config['description']?($temp .= '<span class="help-block m-b-none">'.$config['description'].'</span>'):'';
        return $temp;
    }

    /** 单选 radio
     * @param $config
     * @return string
     */
    private static function n_radio($config){
        $temp = '';
        if (is_array($config['form_view'])){
            $data = explode("\r\n",$config['form_view']['value']);
            foreach ($data as $v){
                $v = explode('|',$v);
                $temp .= '<div class="radio i-checks">';
                $temp .= '<input type="radio" name="config['.$config['mark'].']" '.($v[0] == $config['value']?'checked':'').' value="'.$v[0].'" />'.$v[1];
                $temp .= '</div>';
            }
//            $config['description']?($temp .= '<span class="help-block m-b-none">'.$config['description'].'</span>'):'';
            return $temp;
        }else{
            $temp .= '<div class="radio i-checks">';
            $temp .= '<input type="radio" name="config['.$config['mark'].']" '.(1 == $config['value']?'checked':'').' value="1" />是';
            $temp .= '</div>';
            $temp .= '<div class="radio i-checks">';
            $temp .= '<input type="radio" name="config['.$config['mark'].']" '.(0 == $config['value']?'checked':'').' value="0" />否';
            $temp .= '</div>';
//            $config['description']?($temp .= '<span class="help-block m-b-none">'.$config['description'].'</span>'):'';
        }
        return $temp;
    }

    /** 多选 checkbox
     * @param $config
     * @return string
     */
    private static function n_checkbox($config){
        if (!is_array($config['form_view'])) return '';
        $temp = '';
        $data = explode("\r\n",$config['form_view']['value']);
        $temp .= '<div class="checkbox i-checks">';
        $value = empty($config['value'])?[]:explode(',',$config['value']);
         foreach ($data as $v){
             $v = explode('|',$v);
             $temp .= '<input type="checkbox" name="config['.$config['mark'].'][]" '.(in_array($v[0],$value)?'checked':'').' value="'.$v[0].'" />'.$v[1];
         }
        $temp .= '</div>';
//        $config['description']?($temp .= '<span class="help-block m-b-none">'.$config['description'].'</span>'):'';
         return $temp;
    }

    /** select
     * @param $config
     * @return string
     */
    private static function n_select($config){
        if (!is_array($config['form_view'])) return '';
        $temp = '<select class="form-control" name="config['.$config['mark'].']">';
        $data = explode("\r\n",$config['form_view']['value']);
        foreach ($data as $v){
            $v = explode('|',$v);
            $temp .= '<option value="'.$v[0].'" '.($v[0] == $config['value']?'selected':'').'>'.$v[1].'</option>';
        }
        $temp .= '</select>';
//        $config['description']?($temp .= '<span class="help-block m-b-none">'.$config['description'].'</span>'):'';
        return $temp;
    }





    /** 获取 其 类型
     * @param $type
     * @param null $field
     * @return mixed
     */
    private static function getType($type,$field = NULL){
        foreach (self::$types as $types){
               if ($types['type_id']  == $type){
                   return $field?$types[$field]:$types;
               }
         }
    }

    /** 获取 其 分组
     * @param $group
     * @param null $field
     * @return mixed
     */
    private static function getGroup($group,$field = NULL){
        foreach (self::$groups as $group_value){
            if ($group == $group_value['groups_id']){
                return $field?$group_value[$field]:$group_value;
            }
        }
    }



    /** 归类 数据
     * @param $data
     * @return array
     */
    private static function groupsData($data){
        $tmp    =   [];
        foreach ($data as $v){
            $tmp[$v['groups']][]    =   $v->toArray();
        }
        return $tmp;
    }

}